package com.example.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.my.entity.Permission;
import com.example.my.mapper.PermissionMapper;
import com.example.my.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionServiceImpl
        extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {

    @Override
    public IPage<Permission> getPermissionsByPage(Page<Permission> page) {
        return baseMapper.selectPage(page, null);
    }

    @Override
    public IPage<Permission> getPermissionsByPageWithCondition(Page<Permission> page, String keyword) {
        QueryWrapper<Permission> qw = new QueryWrapper<>();
        qw.like(keyword != null, "perm_name", keyword)
                .or()
                .like(keyword != null, "perm_key", keyword);
        return baseMapper.selectPage(page, qw);
    }

    @Override
    public List<Permission> getPermissionsByUserId(Long userId) {
        return baseMapper.getPermissionsByUserId(userId);
    }

    @Override
    public List<Permission> getPermissionsByRoleId(Long roleId) {
        return null;
    }

    @Override
    public Set<String> getPermissionsByUserIdSet(Long userId) {
        List<Permission> list = getPermissionsByUserId(userId);
        return list.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet()); // 收集成 Set
    }

    @Override
    public Set<String> getPermissionsByRoleIdSet(Long roleId) {
        return null;
    }

    @Override
    public void assignPermissionsToRole(Long roleId, List<Long> permissionIds) {

    }
}